package com.claude.ui;

import java.util.ArrayList;
import java.util.Iterator;

import com.claude.bean.Student;
import com.claude.ui.SlideView.OnSliderListener;
import com.special.ResideMenuDemo.R;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class CheckCompanet {

	private Context mContext = null;
	private ArrayList<MessageItem> mMessageItem = null;
	private OnSliderListener mOnSliderListener = null;
	private DeleteHolderListener mDeleteHolderListener = null;
	private ArrayList<Student> mResource = null;
	public SlideAdapter mSlideAdapter = null;

	public CheckCompanet(Context context, ArrayList<Student> args,
			OnSliderListener onSliderListener,
			DeleteHolderListener deleteHolderListener) {
		mResource = args;
		mContext = context;
		mOnSliderListener = onSliderListener;
		mDeleteHolderListener = deleteHolderListener;
		initResource();
		mSlideAdapter = new SlideAdapter();
	}

	public CheckCompanet(Context context, ArrayList<MessageItem> messageItem, OnSliderListener onSliderListener,
			DeleteHolderListener deleteHolderListener, int tag) {
		mContext = context;
		mOnSliderListener = onSliderListener;
		mDeleteHolderListener = deleteHolderListener;
		mMessageItem = messageItem;
		mSlideAdapter = new SlideAdapter();
	}

	public void setData(ArrayList<MessageItem> messageItem) {
		mMessageItem = messageItem;
	}

	public ArrayList<MessageItem> getData() {
		return mMessageItem;
	}

	public SlideAdapter getAdapter() {
		return mSlideAdapter;
	}

	void initResource() {
		mMessageItem = new ArrayList<CheckCompanet.MessageItem>();
		Iterator<Student> iterator = mResource.iterator();
		while (iterator.hasNext()) {
			Student student = iterator.next();
			MessageItem messageItem = new MessageItem();
			messageItem.title = student.getName();
			messageItem.msg = student.getProfession();
			messageItem.time = student.getStudentNumber();
			messageItem.cardMac = student.getCardMac();
			if (student.getSex().equals("男")) {
				messageItem.iconRes = R.drawable.pic_man;
			} else {
				messageItem.iconRes = R.drawable.pic_women;
			}
			mMessageItem.add(messageItem);
		}
	}

	public class SlideAdapter extends BaseAdapter {

		private LayoutInflater mInflater;

		SlideAdapter() {
			super();
			mInflater = ((Activity) mContext).getLayoutInflater();
		}

		@Override
		public int getCount() {
			return mMessageItem.size();
		}

		@Override
		public Object getItem(int position) {
			return mMessageItem.get(position);
		}

		public void removeItem(int position) {
			mMessageItem.remove(position);
			this.notifyDataSetChanged();
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			final int tposition = position;
			final View tconvertView = convertView;
			final ViewGroup tviewGroup = parent;
			ViewHolder holder;
			SlideView slideView = (SlideView) convertView;
			if (slideView == null) {
				View itemView = mInflater.inflate(R.layout.list_item, null);

				slideView = new SlideView(mContext);
				slideView.setContentView(itemView);

				holder = new ViewHolder(slideView);
				slideView.setOnSliderListener(mOnSliderListener);
				slideView.setTag(holder);
			} else {
				holder = (ViewHolder) slideView.getTag();
			}
			MessageItem item = mMessageItem.get(position);
			item.slideView = slideView;
			item.slideView.shrink();
			holder.icon.setImageResource(item.iconRes);
			holder.title.setText(item.title);
			holder.msg.setText(item.msg);
			holder.time.setText(item.time);
			holder.deleteHolder.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					mDeleteHolderListener.onDelete(tposition, tconvertView,
							tviewGroup, v);
				}
			});
			return slideView;
		}

	}

	public interface DeleteHolderListener {
		public void onDelete(int position, View convertView, ViewGroup parent,
				View v);
	}

	public class MessageItem {
		public int iconRes;
		public String title;
		public String msg;
		public String time;
		public String cardMac;
		public SlideView slideView;
		public String toString(){
			return title;
		}
	}

	private static class ViewHolder {
		public ImageView icon;
		public TextView title;
		public TextView msg;
		public TextView time;
		public ViewGroup deleteHolder;

		ViewHolder(View view) {
			icon = (ImageView) view.findViewById(R.id.icon);
			title = (TextView) view.findViewById(R.id.title);
			msg = (TextView) view.findViewById(R.id.msg);
			time = (TextView) view.findViewById(R.id.time);
			deleteHolder = (ViewGroup) view.findViewById(R.id.holder);
		}
	}
}
